<template>
  <section class="todoapp">
    <Header :list="list" @addFn="addFn"></Header>
    <Main @delFn="delFn" :list="isSelect"></Main>
    <Footer @select="select" @clearFn="clearFn" :list="list"></Footer>
  </section>
</template>

<script>
// 样式引入 + 导入组件并注册使用
import "./styles/index.css";
import "./styles/base.css";
import Header from "./components/TodoHeader.vue";
import Main from "./components/TodoMain.vue";
import Footer from "./components/TodoFooter.vue";
export default {
  methods: {
    delFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    addFn(content) {
      this.list.push({
        id: +new Date(),
        name: content,
        isDone: false,
      });
    },
    clearFn() {
      this.list = this.list.filter((item) => !item.isDone);
    },
    select(type) {
      this.selectType = type;
    },
  },
  computed: {
    isSelect() {
      if (this.selectType === "unsucc") {
        return this.list.filter((item) => !item.isDone);
      } else if (this.selectType === "succ") {
        return this.list.filter((item) => item.isDone);
      } else {
        return this.list;
      }
    },
  },
  components: {
    Header,
    Main,
    Footer,
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("list", JSON.stringify(this.list));
      },
    },
  },
  data() {
    return {
      selectType: "all",
      list: JSON.parse(localStorage.getItem("list")) || [],
    };
  },
};
</script>
